/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.entity.tower;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import towerz.game.entity.Bullet;
import towerz.game.entity.Enemy;
import towerz.game.entity.Entity;
import towerz.game.entity.hut.Hut;
import towerz.system.StateManager;
import towerz.system.Utilities;

/**
 *
 * @author user
 */
public class Tower extends Entity {
	protected Tower() {
		//Initialize
		m_Hut			= null;
		m_Data			= null;
		m_TileX			= 0;
		m_TileY			= 0;
		m_ReloadTimer	= 0;
		m_ShowRange		= false;
		m_Bullets		= new Vector();

		//Sound effect
		StateManager.instance().playSFX("/build.wav");
	}

	public Tower(TowerData data, Hut hut, int x, int y) {
		//Default
		this();

		//Save data
		m_Hut	= hut;
		m_TileX = x;
		m_TileY = y;
		m_Data	= data;
	}

	public TowerData getData() {
		return m_Data;
	}

	public Hut getHut() {
		return m_Hut;
	}

	public void setRangeShowing(boolean showRange) {
		m_ShowRange = showRange;
	}

	public Bullet[] getBullets() {
		//Initialize
		Bullet[] Bullets = null;

		//If not empty
		if (!m_Bullets.isEmpty()) {
			//Add bullet
			Bullets = new Bullet[m_Bullets.size()];
			for (int i = 0; i < m_Bullets.size(); i++) Bullets[i] = (Bullet)m_Bullets.elementAt(i);
		}
		m_Bullets.removeAllElements();

		//Return the array
		return Bullets;
	}

	public void update(long time, Vector enemies) {
		//Reduce reload
		if (m_ReloadTimer > 0) m_ReloadTimer -= time;
		else {
			//Calculate range
			long TowerRange = m_Data.getRange() * m_Data.getRange();

			//If locked on enemy exist
			if (m_Locked != null) {
				//Is it exist?
				if (enemies.contains(m_Locked)) {
					//Check range
					int X		= m_Locked.getX() - getX();
					int Y		= m_Locked.getY() - getY();
					long Range	= (X * X) + (Y * Y);
					if (Range > TowerRange) {
						m_Locked = null;
					}
				} else m_Locked = null;
			}

			//If there's no locked enemy
			if (m_Locked == null) {
				long Closest = -1;

				//For each enemy
				for (int i = 0; i < enemies.size(); i++) {
					//Calculate range
					int X		= ((Enemy) enemies.elementAt(i)).getX() - getX();
					int Y		= ((Enemy) enemies.elementAt(i)).getY() - getY();
					long Range	= (X * X) + (Y * Y);

					//Set as locked if closest
					if (Closest < 0 || Range < Closest) {
						Closest = Range;
						m_Locked = (Enemy) enemies.elementAt(i);
					}
				}

				//Check if locked is in range
				if (m_Locked != null) {
					if (Closest > TowerRange) m_Locked = null;
				}
			}

			//If locked enemy exist
			if (m_Locked != null) {
				//Create bullet
				m_Bullets.addElement(new Bullet(m_Data.getBullet(), m_Locked, getX() + getData().getImage().getWidth()/2, getY(), m_Hut.getData().getBonusDmg()));

				//Load next shot
				m_ReloadTimer += getReload();
			}
		}
	}

	public long getReload(){
		return (long) (m_Data.getReload() * m_Hut.getData().getBonusSpd());
	}

	public void draw(Graphics g, int startX, int startY, int endX, int endY, int offsetX, int offsetY) {
		//Skip if no image
		if (m_Data.getImage() == null) return;

		//Ensure in drawing area
		if (m_TileX < startX)	return;
		if (m_TileY < startY)	return;
		if (m_TileX > endX)		return;

		//Calculate draw position
		int X = ((m_TileX - startX) * Utilities.TILE_WIDTH)  + offsetX + (Utilities.TILE_WIDTH / 2);
		int Y = ((m_TileY - startY) * Utilities.TILE_HEIGHT) + offsetY +  Utilities.TILE_HEIGHT;

		if (Y - m_Data.getImage().getHeight() > ((endY + 1) * Utilities.TILE_HEIGHT) + offsetY) return;

		//Draw
		if (m_ShowRange) g.drawImage(m_Data.getRangeArea(), X, Y, Graphics.HCENTER | Graphics.VCENTER);
		g.drawImage(m_Data.getImage(), X, Y, Graphics.HCENTER | Graphics.BOTTOM);
	}

	//Data
	protected TowerData m_Data;

	//Other
	protected Hut		m_Hut;
	protected Enemy		m_Locked;
	protected Vector	m_Bullets;
	protected boolean	m_ShowRange;
	protected long		m_ReloadTimer;
}
